<?php

namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Post;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;//缓存
use Illuminate\Support\Facades\Gate;

class LuckmoneyController extends Controller{

    public function index(Request $request){
        $data = DB::table('luckmoney')
            ->select('*')
            ->where(function($query){
                $query -> where('is_del', '0');
            })
            ->orderBy('id', 'desc')
            ->paginate(10);
        foreach($data as $key => $value){
            $join_number = DB::table('luckmoney_log')->where('luckmoney_id', $value->id)->count();    // 加入人数
            $value->join_number = $join_number;
        }
        return view('admin.luckmoney.index',compact('data'));
    }

    public function create(){
        return view('admin.luckmoney.add',compact('data'));
    }

    public function store(Request $request){
        $arr = [
            'name' => $request->name,
            'start_time' => strtotime($request->start_time),
            'end_time' => strtotime($request->end_time),
            'number' => $request->number,
            'use_number' => '0',
            'share_number' => $request->share_number,
            'money' => $request->money,
            'spare_number' => $request->spare_number,
            'title_array' => json_encode($request->title),
            'share_img' => $request->share_img,
            'background_img' => $request->background_img,
            'info' => $request->info,
            'is_close' => $request->is_close,
            'created_at' => time()
        ];
        DB::table('luckmoney')->insert($arr);
        return redirect('camelredadmin/luckmoney/');
    }

    public function edit($id){
        $data = DB::table('luckmoney')
            ->select('*')
            ->where(function($query)use($id){
                $query -> where('id', $id);
                $query -> where('is_del', '0');
            })->first();
        $data->title_array = json_decode($data->title_array);
        return view('admin.luckmoney.edit',compact('data','id'));
    }

    public function update(Request $request,$id){
        $res = DB::table('luckmoney')->where('id',$id)->update([
            'name' => $request->name,
            'start_time' => strtotime($request->start_time),
            'end_time' => strtotime($request->end_time),
            'number' => $request->number,
            'use_number' => '0',
            'share_number' => $request->share_number,
            'money' => $request->money,
            'spare_number' => $request->spare_number,
            'title_array' => json_encode($request->title),
            'share_img' => $request->share_img,
            'background_img' => $request->background_img,
            'info' => $request->info,
            'is_close' => $request->is_close,
            'updated_at' => time()
        ]);
        if ($res == 1) {
            return redirect('camelredadmin/luckmoney/');
        }else{
            return back()->with('更新失败！');
        }
    }

    public function destroy($id){
        $post = DB::table('luckmoney')->where('id', $id)->update([
            'is_del' => '1',
            'updated_at' => time()
        ]);
        return  $data = ['status' => 0,'msg' => '删除成功！'];
    }

    public function changeState(Request $request){
//        print_r(1);exit;
        $id = $request->id;
        $is_close = $request->isClose;
        $post = DB::table('luckmoney')->where('id', $id)->update([
            'is_close' => $is_close,
            'updated_at' => time()
        ]);
        return  $data = ['status' => 0,'msg' => '成功！'];
    }




}
